package com.weixin.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import com.weixin.bean.Food;

import com.weixin.service.FoodService;

import com.weixin.service.impl.FoodServiceimpl;

import com.weixin.util.Page;

@WebServlet("/FoodServlet/*")
public class FoodServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private FoodService foodService = new FoodServiceimpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String pathInfo = request.getPathInfo().substring(1);
		if (pathInfo.equals("listFood")) {
			listFood(request, response);
		} else if ("listAllJsonSearch".equals(pathInfo)) {
			listAllJsonSearch(request, response);
		} else if ("addFood".equals(pathInfo)) {
			addFood(request, response);
		} else if ("removeByIdJson".equals(pathInfo)) {
			removeByIdJson(request, response);
		} else if ("updateFood".equals(pathInfo)) {
			updateFood(request, response);
		} else if ("foodDetail".equals(pathInfo)) {
			foodDetail(request, response);
		} else if ("dianFood".equals(pathInfo)) {
			dianFood(request, response);
		}
	}

	private void dianFood(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String foodName = request.getParameter("foodName");
		Food food = foodService.findByfoodName(foodName);
		HttpSession session = request.getSession();
		session.setAttribute("food", food);
		response.getWriter().print(1);
	}

	private void foodDetail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Food> foodList = foodService.selectAllbypage(pageNum, Page.pageSize);
		request.setAttribute("foodList", foodList);
		int total = foodService.getTotal();
		int pageCount = (int) Math.ceil(total * 1.0 / Page.pageSize);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("zhuohao", request.getParameter("zhuohao"));
		request.getRequestDispatcher("/app/caidan.jsp").forward(request, response);

	}

	private void updateFood(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int foodId = Integer.valueOf(request.getParameter("foodId"));
		double price = Double.valueOf(request.getParameter("price"));
		double vprice = Double.valueOf(request.getParameter("vprice"));
		boolean b = foodService.update(foodId, price, vprice);
		if (b) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}

	}

	private void removeByIdJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));
		if (foodService.delete(id)) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}
	}

	private void addFood(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		Food food = new Food();
		int foodTypeid = Integer.valueOf(request.getParameter("foodTypeid"));
		food.setFoodTypeid(foodTypeid);
		food.setFoodName(request.getParameter("foodName"));
		double price = Double.valueOf(request.getParameter("price"));
		food.setPrice(price);
		double vprice = Double.valueOf(request.getParameter("vprice"));
		food.setVprice(vprice);
		food.setIntroduction(request.getParameter("introduction"));
		food.setImg(request.getParameter("img"));
		boolean b = foodService.addFood(food);
		if (b) {
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}

	}

	private void listAllJsonSearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String search = request.getParameter("search");

		// 接收分页参数：
		// 起始索引
		int offset = Integer.valueOf(request.getParameter("offset"));
		// 每页大小
		int limit = Integer.valueOf(request.getParameter("limit"));

		// 服务器端分页：
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 菜品列表数据:
		List<Food> foodList = foodService.listAllByPageSearch(offset, limit, search);
		int total = foodService.getTotal();
		Map<String, Object> map = new HashMap();
		map.put("total", total);
		map.put("rows", foodList);
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(map));
	}

	private void listFood(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/javascript; charset=utf-8");
		List<Food> foodList = foodService.getAll();
		int total = foodService.getTotal();
		Map map = new HashMap();
		map.put("total", foodList.size());
		map.put("rows", foodList);
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(map));
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
